<?php	
	include('db_connection.php');
	class scheduled_trainee{
            
            function add_trainer_scheduled($traniee_schedule_id = ''){
                $chk_entry = mysql_query("select * from tbl_trainee_schedule where student_id = ".$traniee_schedule_id);
                $result_entry = mysql_fetch_assoc($chk_entry);
                if($result_entry['id'] != ''){
                    $update_query = "UPDATE `tbl_trainee_schedule` SET `trainer_id`='".$_POST['trainer_name']."',`student_id`='".$_POST['student_name']."',`course_id`='".$_POST['student_course']."' WHERE id = '".$result_entry['id']."'";
                    mysql_query($update_query);
                    $msg = 'Schedule Updated successfully';
                }else{
                    $add_query = "INSERT INTO `tbl_trainee_schedule`(`id`, `trainer_id`, `student_id`, `course_id`,  `create_ts`) VALUES ('NULL','".$_POST['trainer_name']."','".$_POST['student_name']."','".$_POST['student_course']."','".date('Y-m-d H:i:s')."')";
                    mysql_query($add_query);
                   $msg = 'Schedule Added successfully';
                }
                
                
                
                $tranier_query = mysql_query("select * from tbl_admin 
                    where is_trainer = 1 and id = '".$_POST['trainer_name']."' ");
                   $traniee_data = mysql_fetch_assoc($tranier_query);
                   
                   
                   
                   
                /* This funcion is used for  sent mail. ends here */
                    $trainer_name = $tranier_query['name'];
                    $trainer_skyid = $tranier_query['skype_mail'];
                    ob_start();
                    include_once('../../mailtemplate/assigned_trainer.php');
                    $mail_body = ob_get_contents();
                    ob_end_clean();
                     $mail_to = $_POST['student_email'];
                     $mail_subject="Trainee Detail";
                    // $mail_body = "Admin assigned Tranieer for you.Your Tranieer detail as below<br/>\r\n";
                    // $mail_body.="Trainer name = ".$tranier_query['name']."<br/>\r\n";
                    // $mail_body.="Skype Id = ".$tranier_query['skype_mail']."<br/>\r\n";
                     $headers  = 'MIME-Version: 1.0' . "\r\n";
                     $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
                     $headers .= 'From:RV Training<info@rvtraining.net>' . "\r\n";
                     mail($mail_to,$mail_subject,$mail_body, $headers);
                return $msg;
                
            }
            function student_array(){
                $student_query = mysql_query("select * from tbl_user where is_paid = 1 and is_active = 1 order by username");
                $finalArray = array();
                while($student_data = mysql_fetch_array($student_query))
                {
                    $finalArray[$student_data['id']]['name'] = ucfirst($student_data['username']);
                    $finalArray[$student_data['id']]['email'] = $student_data['email'];
                }
                 return $finalArray;
            }
            
            function course_array($student_id = ""){
                if($student_id != ''){
                    $where = "where a.user_id = '".$student_id."'";
                }else{
                    $where = "";
                }
                $course_query = mysql_query("select *,b.id as course_id from tbl_user_details a
                    Join tbl_courses b ON a.course_id = b.id ".$where."
                     order by course_name");
                $finalArray = array();
                while($course_data = mysql_fetch_array($course_query))
                {
                    $finalArray[$course_data['course_id']] = ucfirst($course_data['course_name']);
                }
                 return $finalArray;
            }
            
            function traniee_array($course_id = ''){
                if($course_id != ''){
                    $where = "and a.course = '".$course_id."'";
                }else{
                    $where = "";
                }
                
                $course_query = mysql_query("select *,b.id as course_id,a.id as admin_id from tbl_admin a
                    Join tbl_courses b ON a.course = b.id 
                    where is_trainer = 1 ".$where."  order by course_name ");
                $finalArray = array();
                while($course_data = mysql_fetch_array($course_query))
                {
                    $finalArray[$course_data['admin_id']] = ucfirst($course_data['Name']);
                }
                 return $finalArray;
            }
            
            function traniee_scheduled_array($where = "",$offset = "" ,$limit = ""){
                $page_cond = '';
                if($limit != ''){
                  $page_cond = "LIMIT " . $offset . ", " . $limit;
                }
                /*
                $query = mysql_query("SELECT b.`username`,b.`email`,c.`course_name`,d.name,a.* FROM  tbl_trainee_schedule a
JOIN tbl_user b ON a.`student_id` = b.id JOIN tbl_courses c ON a.`course_id` = c.`id` 
JOIN tbl_admin d ON a.`trainer_id` = d.id
".$where." ".$page_cond." ");*/
                
                
                $query = mysql_query("
SELECT u.`username`,u.`email`,c.`course_name`,e.name,a.id as user_detail_id,a.course_id as user_course_id ,d.* 
FROM tbl_user_details a
JOIN tbl_user u ON a.`user_id` = u.id 
JOIN tbl_courses c ON a.`course_id` = c.`id` 
LEFT JOIN  tbl_trainee_schedule d ON d.student_id = a.id
LEFT JOIN tbl_admin e ON d.`trainer_id` = e.id
WHERE (a.via_paid = 1 OR a.via_paid = 2 OR a.via_paid = 3 ) and a.is_ebook = 0 ".$where." ".$page_cond."");
                $finalArray = array();
                if(mysql_num_rows($query) > 0 ){
                    while ($row = mysql_fetch_array($query)){
                        $finalArray[] = $row;
                    }
                }
                return $finalArray;
            }
	}
?>